package com.tts.repository;

import com.tts.entity.BorrowRecord;
import com.tts.entity.BorrowRecord.BorrowStatus;
import com.tts.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface BorrowRecordRepository extends JpaRepository<BorrowRecord, Long> {
    List<BorrowRecord> findByUserId(Long userId);
    
    List<BorrowRecord> findByUserIdAndStatus(Long userId, BorrowStatus status);
    
    @Query("SELECT br FROM BorrowRecord br WHERE br.status = 'BORROWED' AND br.shouldReturnTime < :now")
    List<BorrowRecord> findOverdueRecords(LocalDateTime now);
    
    @Query("SELECT COUNT(br) FROM BorrowRecord br WHERE br.user.id = :userId AND br.status = 'BORROWED'")
    long countCurrentBorrowsByUserId(Long userId);
    
    List<BorrowRecord> findByBookId(Long bookId);

    List<BorrowRecord> findByUser(User user);

    List<BorrowRecord> findByBorrowTimeBetween(LocalDateTime start, LocalDateTime end);
} 